package com.ithong.controller;

import com.ithong.entity.User;
import com.ithong.service.UserService;
import com.springmvc.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
public class UserController {

    @AutoWired
    UserService userService;

    @RequestMapping("/user/query")
    public String findUsers(HttpServletRequest request, HttpServletResponse response,@RequestParam("name") String name){
        response.setContentType("text/html;charset=utf-8");
//        try {
//            List<User> users = userService.findUsers(name);
//            PrintWriter out = response.getWriter();
//            out.print("<h1>SpringMvc控制器:"+name+"</h1>");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        String userMessage = userService.getUserMessage(name);
        request.setAttribute("userMessage",userMessage);
        // 转发到user.jsp SpringMvc默认就是转发
        return "forward:/user.jsp";
    }

    @RequestMapping("/user/queryjson")
    @ResponseBoby // 返回值的自动转为JSON数据
    public List<User> queryUsers(HttpServletRequest request,HttpServletResponse response,String name){
        return userService.findUsers(name);
    }
}
